package com.zimmur.service.impl.zone;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.common.AppModuleEnum;
import com.zimmur.common.ConstData;
import com.zimmur.common.EnumPointRule;
import com.zimmur.dao.zone.ITopicCommentDao;
import com.zimmur.dao.zone.ITopicDao;
import com.zimmur.model.zone.SharedProperty;
import com.zimmur.model.zone.Topic;
import com.zimmur.model.zone.TopicComment;
import com.zimmur.service.IUserService;
import com.zimmur.service.account.IPointLogService;
import com.zimmur.service.account.IUserMessageService;
import com.zimmur.service.zone.ITopicCommentService;
  
  
@Service("topicCommentService")  
public class TopicCommentServiceImpl implements ITopicCommentService {  
	private static Logger log=LoggerFactory.getLogger(TopicCommentServiceImpl.class);
	@Resource  
    private ITopicCommentDao topicCommentDao;
    @Resource  
    private ITopicDao topicDao;
	@Resource
    private IPointLogService pointService;
    @Resource  
    private IUserService  userService;
    @Resource
    private IUserMessageService userMessageService;
    
	@Override
	@Transactional
	public int insertTopicComment(TopicComment topicComment) {
		Integer resut = 0;
		
		TopicComment userTopicComment=null;
		try {
			if(topicComment.getUserCommentId()!=0){
				userTopicComment=topicCommentDao.selectByCommentId(topicComment.getUserCommentId());
				if(userTopicComment!=null){
					topicComment.setUserCode(userTopicComment.getCreateCode());
			        topicComment.setUserName(userTopicComment.getCreateUserName());
			        topicComment.setUserCategoryName(userTopicComment.getCreateCategoryName());
			        topicComment.setUserCommentContent(userTopicComment.getContent());
			        topicComment.setUserId(userTopicComment.getCreateUserId());
			        topicComment.setReplyCommentType(userTopicComment.getCommentType());
				}
			}
	        resut= topicCommentDao.insertTopicComment(topicComment);
			
			Topic topic= topicDao.selectByIdstopic(topicComment.getTopicId());
			if(topic != null){
				/*评论*/
		        Topic topics =new Topic();
		        Integer commentNum= topic.getCommentNum()+1;
		        topics.setCommentNum(commentNum);
		        topics.setTopicId(topicComment.getTopicId());
		        topicDao.updateStopicCommentNum(topics);
		        
		        topicCommentPoint(topicComment.getCreateUserId(),topicComment.getCreateCode());
		        
		        if(resut>0){
		        	//GoodsUser userInfo = userService.selectByUserId(topicComment.getCreateUserId());
		        	//给话题发布者发送消息
	        		String strTitle = topicComment.getCreateUserName()+"评论了你的话题!";
		        	String strMessage = topicComment.getCreateUserName()+"评论了你的话题<"+topic.getTitle()+">.";
					userMessageService.sendMessage(topicComment.getCreateUserId(), topic.getCreateUser(), 
							strTitle, strMessage,String.valueOf(topic.getTopicId()), ConstData.MESSAGE_CATEGORY_NOTIFY,Integer.valueOf(AppModuleEnum.Zone.getModuleId()));
						
					//给被评论者发送消息
		        	if(userTopicComment!=null ){
		        		String strTitle1 = topicComment.getCreateUserName()+"评论了你的回复!";
			        	String strMessage1 =  topicComment.getCreateUserName()+"评论了你的回复<"+topic.getTitle()+">.";
						userMessageService.sendMessage(topicComment.getCreateUserId(), topic.getCreateUser(), 
								strTitle1, strMessage1,String.valueOf(topic.getTopicId()), ConstData.MESSAGE_CATEGORY_NOTIFY,Integer.valueOf(AppModuleEnum.Zone.getModuleId()));
		        	}
		        }
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return resut;
	}
    /**
     * 用户话题被评论,积分增加规则
     * @param userId
     */
	private void topicCommentPoint(long userId,String categoryCode){
		if(userId>0){
    		//begin 积分规则处理
    		EnumPointRule pointRule = EnumPointRule.ZK_TOPIC_REPLY;
    		if(categoryCode.equals("xk"))
    			pointRule = EnumPointRule.XK_REPLY;
//			GoodsUser fromUser = userService.selectByUserId(userId);
    		
			pointService.mainPointLog(userId, "话题回复",pointRule, 0);
		}
	}
	
	/**
	 * 获取话题全部评论
	 * 	
	 */
	public List<TopicComment> pageCommentList(long topicId,long userId, Integer pageNow) {
		List<TopicComment> commentLists=null;
		try {
				if(pageNow == 0){
					pageNow=1;
				}
				SharedProperty sp=new SharedProperty();
				sp.setUserId(userId);
		    	//sp.setCreateUser(createUser);
		    	sp.setTopicId(topicId);
		    	sp.setPageNow(pageNow);
		    	commentLists=topicCommentDao.findTopicCommentList(sp);
			
		
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return commentLists;
	}

	@Override
	public TreeSet<Long> selectByCreateUserTopicCommentSet(SharedProperty sharedProperty) {
		return topicCommentDao.selectByCreateUserTopicCommentSet(sharedProperty);
	}

	@Override
	public Integer countByTopicIdTopicComment(long topicId) {
		return topicCommentDao.countByTopicIdTopicComment(topicId);
	}
	@Override
	public boolean updateCommentRewardStatus(long topicId,long commentId, int rewardStatus) {
		if(commentId>0){			
			Map<String, Object> map = new HashMap<>();
			map.put("commentId", commentId);
			map.put("rewardStatus", rewardStatus);
			return topicCommentDao.updateCommentRewardStatus(map)>0?true:false;
		}
		return false;
	}
	@Override
	public List<TopicComment> pageHotCommentList(long topicId, long userId,Integer pageNow) {
		List<TopicComment> commentLists=null;
		
		SharedProperty sp=new SharedProperty();
		sp.setPageNow(pageNow);
    	sp.setUserId(userId);
    	sp.setTopicId(topicId);			    	
    	commentLists=topicCommentDao.findHotTopicCommentList(sp);				
		return commentLists;
	}
	@Override
	public boolean updateCommentPressNum(Long commentId, int praise) {
		try {
			TopicComment topicComment=topicCommentDao.selectByCommentId(commentId);
			Integer pressNum=topicComment.getPressNum()==null?0:topicComment.getPressNum();
			topicComment.setPressNum(pressNum+praise);
			return topicCommentDao.updateByPrimaryKeySelective(topicComment)>0?true:false;
		} catch (Exception e) {
			log.error(e.getMessage());
			return false;
		}
		
	}
	
   
	 
}  
